package com.example.demo.controller;

import com.example.demo.service.CaseManagementService;
import com.example.demo.service.DecisionService;
import com.example.demo.service.ProcessService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 三种引擎演示控制器
 */
@RestController
@RequestMapping("/api/engine-demo")
public class EngineDemoController {

    @Autowired
    private ProcessService processService;


    @Autowired
    private CaseManagementService caseManagementService;

    /**
     * BPMN引擎演示：启动请假流程
     */
    @PostMapping("/bpmn/start-leave-process")
    public Map<String, Object> startLeaveProcess(@RequestBody Map<String, Object> request) {
        String employee = (String) request.get("employee");
        String reason = (String) request.get("reason");
        Integer days = (Integer) request.get("days");
        
        String processInstanceId = processService.startLeaveRequest(employee, reason, days);
        
        Map<String, Object> response = new HashMap<>();
        response.put("message", "BPMN流程已启动");
        response.put("processInstanceId", processInstanceId);
        response.put("engine", "BPMN");
        response.put("description", "结构化业务流程，按预定义路径执行");
        
        return response;
    }

    /**
     * DMN引擎演示：执行决策规则
     */
    @PostMapping("/dmn/execute-decision")
    public Map<String, Object> executeDecision(@RequestBody Map<String, Object> request) {
        String employee = (String) request.get("employee");
        String reason = (String) request.get("reason");
        Integer days = (Integer) request.get("days");
        
        // 模拟DMN决策执行
        Map<String, Object> decisionInput = new HashMap<>();
        decisionInput.put("employee", employee);
        decisionInput.put("reason", reason);
        decisionInput.put("days", days);
        
        // 这里可以调用DMN决策服务
        // 为了演示，我们返回模拟结果
        Map<String, Object> decisionResult = new HashMap<>();
        decisionResult.put("approvalResult", days <= 3 ? "自动批准" : "需要审批");
        decisionResult.put("approverLevel", days <= 3 ? "直接主管" : "部门经理");
        decisionResult.put("needHRApproval", days > 5);
        
        Map<String, Object> response = new HashMap<>();
        response.put("message", "DMN决策已执行");
        response.put("decisionResult", decisionResult);
        response.put("engine", "DMN");
        response.put("description", "业务规则决策，基于输入条件自动判断");
        
        return response;
    }

    /**
     * CMMN引擎演示：启动客户投诉案例
     */
    @PostMapping("/cmmn/start-complaint-case")
    public Map<String, Object> startComplaintCase(@RequestBody Map<String, Object> request) {
        String customerName = (String) request.get("customerName");
        String complaintDescription = (String) request.get("complaintDescription");
        String complaintHandler = (String) request.get("complaintHandler");
        String priority = (String) request.get("priority");
        
        String caseInstanceId = caseManagementService.startComplaintCase(
            customerName, complaintDescription, complaintHandler, priority);
        
        Map<String, Object> response = new HashMap<>();
        response.put("message", "CMMN案例已启动");
        response.put("caseInstanceId", caseInstanceId);
        response.put("engine", "CMMN");
        response.put("description", "非结构化案例管理，灵活处理复杂业务场景");
        
        return response;
    }

    /**
     * 获取案例状态
     */
    @GetMapping("/cmmn/case-status/{caseInstanceId}")
    public Map<String, Object> getCaseStatus(@PathVariable String caseInstanceId) {
        caseManagementService.getCaseStatus(caseInstanceId);
        
        Map<String, Object> response = new HashMap<>();
        response.put("message", "案例状态已查询");
        response.put("caseInstanceId", caseInstanceId);
        
        return response;
    }

    /**
     * 完成案例任务
     */
    @PostMapping("/cmmn/complete-task")
    public Map<String, Object> completeCaseTask(@RequestBody Map<String, Object> request) {
        String taskId = (String) request.get("taskId");
        @SuppressWarnings("unchecked")
        Map<String, Object> variables = (Map<String, Object>) request.get("variables");
        
        caseManagementService.completeCaseTask(taskId, variables);
        
        Map<String, Object> response = new HashMap<>();
        response.put("message", "案例任务已完成");
        response.put("taskId", taskId);
        
        return response;
    }

    /**
     * 三种引擎对比说明
     */
    @GetMapping("/comparison")
    public Map<String, Object> getEngineComparison() {
        Map<String, Object> comparison = new HashMap<>();
        
        Map<String, Object> bpmn = new HashMap<>();
        bpmn.put("name", "BPMN - 业务流程建模与标记");
        bpmn.put("purpose", "结构化业务流程执行");
        bpmn.put("characteristics", List.of("顺序性", "可预测", "标准化", "自动化"));
        bpmn.put("useCase", "请假审批、订单处理、报销流程");
        bpmn.put("example", "员工提交请假申请 → 经理审批 → 发送通知");
        
        Map<String, Object> dmn = new HashMap<>();
        dmn.put("name", "DMN - 决策模型与标记");
        dmn.put("purpose", "业务规则和决策逻辑");
        dmn.put("characteristics", List.of("规则导向", "可重用", "可维护", "透明化"));
        dmn.put("useCase", "风险评估、定价策略、审批规则");
        dmn.put("example", "根据员工级别、请假天数、请假类型自动决定审批流程");
        
        Map<String, Object> cmmn = new HashMap<>();
        cmmn.put("name", "CMMN - 案例管理模型与标记");
        cmmn.put("purpose", "非结构化案例管理");
        cmmn.put("characteristics", List.of("灵活性", "动态性", "用户驱动", "适应性"));
        cmmn.put("useCase", "客户投诉、法律案件、医疗诊断");
        cmmn.put("example", "客户投诉案例：接收投诉 → 调查处理 → 制定方案 → 跟进回访");
        
        comparison.put("bpmn", bpmn);
        comparison.put("dmn", dmn);
        comparison.put("cmmn", cmmn);
        
        return comparison;
    }
}
